
# generate figure 1

library(rio) # to load data
library(tidyverse) # to reshape data
library(zoo) # to work with dates
library(scales) # to make nice axis breaks
library(cowplot) # to arrange plots as grids

# set your working directory
setwd("~/replication_files")


ecuador1 <- import("data/monthly_data.csv") %>%
  filter(iso3c == "ECU") %>%
  mutate(date = as.yearmon(paste(month, year, sep = "/"), "%m/%Y")) %>%
  select(date,embi_global) %>%
  ggplot(aes(x = date, y=embi_global)) + geom_line() + theme_classic() + 
  labs(x = "Year", y = "Basis Points", title = "EMBI Global Spreads for Ecuador") + 
  theme(axis.text = element_text(size = 12), plot.title = element_text(face = "bold"),
        axis.title = element_text(size = 12)) + 
  scale_x_continuous(breaks = pretty_breaks(), limits = c(1996,2020)) +
  annotate(geom = "rect", xmin = 2008.99, xmax = 2011.99, ymin = 0, ymax = Inf,
           fill = "gray", alpha = 0.5)


ecuador2 <- import("data/monthly_data.csv") %>%
  filter(iso3c == "ECU") %>%
  mutate(date = as.yearmon(paste(month, year, sep = "/"), "%m/%Y")) %>%
  select(date,sovereign_issued_1yr_amount) %>%
  mutate(sovereign_issued_1yr_amount = sovereign_issued_1yr_amount/1000000000)  %>% # in billions
  ggplot(aes(x = date, y=sovereign_issued_1yr_amount)) + geom_col() + theme_classic() + 
  labs(x = "Year",y = "Billions of Constant US$", title = "Sovereign Bonds Issued by Ecuador") + 
  theme(axis.text = element_text(size = 12), plot.title = element_text(face = "bold"),
        axis.title = element_text(size = 12)) + 
  scale_x_continuous(breaks = pretty_breaks(), limits = c(1996,2020)) +
  annotate(geom = "rect", xmin = 2008.99, xmax = 2011.99, ymin = 0, ymax = Inf,
           fill = "gray", alpha = 0.5)

plot_grid(ecuador1,ecuador2, nrow =  2, align = "v")

ggsave("figures/figure1.pdf", height = 7, width = 6)
